package my.action.article;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.controller.Action;
import my.controller.ActionForward;
import my.db.DAO.ArticleDAO;
import my.db.DTO.ArticleDTO;

public class PageLoadAction implements Action{
	@Override
	public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception {
	   	ActionForward forward=new ActionForward();
	   	ArrayList<ArticleDTO> list = new ArrayList<ArticleDTO>();
	   	ArrayList<ArticleDTO> list_recent=new ArrayList<>();
	   	
		ArticleDAO dao = new ArticleDAO();
		int start, stop, rows, pages;
		String keyword;
		
		rows = Integer.parseInt(request.getParameter("rows"));
		pages = Integer.parseInt(request.getParameter("pages"));
		keyword=request.getParameter("keyword");
		start = (rows * (pages - 1)) + 1;
		stop = rows * pages;
		//List all records or search

		list=dao.listPagination(start, stop, keyword);
		list_recent=dao.mostRecent();
		
		request.setAttribute("article_mostrecent", list_recent);
		request.setAttribute("article_list", list);
		forward.setPath("index.jsp");
		forward.setRedirect(false);
		return forward;
	}
}
